<?php

namespace common\models\goods;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\goods\GoodsAttr;

/**
 * GoodsAttrSearch represents the model behind the search form about `common\models\GoodsAttr`.
 */
class GoodsAttrSearch extends GoodsAttr
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'input_type', 'type', 'is_aliases', 'is_upload_img', 'is_color', 'is_not_null'], 'integer'],
            [['name', 'values'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = GoodsAttr::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'input_type' => $this->input_type,
            'type' => $this->type,
            'is_aliases' => $this->is_aliases,
            'is_upload_img' => $this->is_upload_img,
            'is_color' => $this->is_color,
            'is_not_null' => $this->is_not_null,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'values', $this->values]);

        return $dataProvider;
    }
}
